/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabaseConnection;

import Entities.Employee;
import com.sun.org.apache.bcel.internal.util.Class2HTML;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author serhat
 */
public class Connector {

    private final String user = "root";
    private final String password = "serhat1991";
    private final String url = "jdbc:mysql://127.0.0.1/otobus.db";
    protected Connection connection;
    protected Statement statement;
    
    protected  ResultSet executeQuery(String query) throws SQLException{
        ResultSet resultSet;
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        return resultSet;
    }
    public  int executeUpdate(String query) throws SQLException{
        
        statement = connection.createStatement();
        int result = statement.executeUpdate(query);
        return result;
        
    }
    
    public  int executeUpdateAndReturnKey(String query) throws SQLException{
        
        int result = 0;
        statement = connection.createStatement();

        statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
        
        ResultSet rs = statement.getGeneratedKeys();
        if (rs.next()) {
            result = rs.getInt(1);
        }

        return result;
        
    }
   
    public Connector() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(url, user, password);
        
    }
    
    
    
}
